package com.zj.leetcode.easy.hash;

import java.util.HashMap;

/**
 * @program: algorithm
 * @description: 单词规律
 * 290 word-pattern
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class WordPattern {
    public static void main(String[] args) {
//        String pattern = "abbb";
//        String s = "dog cat cat fish";

        String pattern = "abbc";
        String s = "dog cat cat cat";
        Solution solution = new WordPattern().new Solution();
        System.out.println(solution.wordPattern(pattern, s));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean wordPattern(String pattern, String s) {

            HashMap<Object, Integer> map = new HashMap<>();
            String[] split = s.split(" ");
            if (split.length != pattern.length()) {
                return false;
            }
            for (Integer i = 0; i < split.length; i++) {
                if(map.put(pattern.charAt(i), i) != map.put(split[i], i)){
                    return false;
                }
            }
            return true;
        }


        /**
         * map 竟然有这样的方法，
         * containsValue，绝绝子
         * @param pattern
         * @param s
         * @return
         */
        public boolean wordPattern02(String pattern, String s) {

            HashMap<String, Character> map = new HashMap<>();
            String[] split = s.split(" ");
            if (split.length != pattern.length()) {
                return false;
            }
            for (int i = 0; i < split.length; i++) {
                if(map.containsKey(split[i])){
                    Character temp = map.get(split[i]);
                    if(temp != pattern.charAt(i)){
                        return false;
                    }
                } else {
                    if(map.containsValue(pattern.charAt(i))){
                        return false;
                    } else {
                        map.put(split[i], pattern.charAt(i));
                    }

                }
            }
            return true;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
